import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:matisse_android_plugin/export_matisse.dart';
import 'package:matisse_android_plugin/src/ios/choose_matisse_type_dialog.dart';
import 'package:multi_image_picker/multi_image_picker.dart';
import 'package:path_provider/path_provider.dart';

///ios临时选择器
class IosMatisseUtil {
  ///ios 选择所有 （弹出选择框）
  static Future<List<String>> selectIosAll(BuildContext context,
      {final int maxImageSize,
        final int maxVideoSize,
        final bool isCanMixSelect,
        final ScreenOrientation orientation,
        final int spanCount}) async {
    File file;
    if (context == null) {
      file = await ImagePicker.pickImage(source: ImageSource.gallery);
      return Future.value([file?.path]);
    }
    int _tmpMaxChoose;
    if(isCanMixSelect == true){
      _tmpMaxChoose = maxImageSize > maxVideoSize ? maxVideoSize : maxImageSize;
    }
    ClickType type = await ChooseMatisseTypeUtil.showChooseTypeDialog(
      context,
      maxImageSize: _tmpMaxChoose ?? maxImageSize,
      maxVideoSize: _tmpMaxChoose ?? maxVideoSize,
    );

    if (type == ClickType.camera) {
      file = await ImagePicker.pickImage(source: ImageSource.camera);
    } else if (type == ClickType.gallery) {
      return await selectIosPhoto(context,
          maxSize: _tmpMaxChoose ?? maxImageSize,
          orientation: orientation,
          spanCount: spanCount);
    } else if (type == ClickType.video) {
      file = await ImagePicker.pickVideo(source: ImageSource.gallery);
    }
    return Future.value([file?.path]);
  }

  ///ios 获取图片
  static Future<List<String>> selectIosPhoto(BuildContext context,
      {final int maxSize,
        final ScreenOrientation orientation,
        final int spanCount}) async {
//    File file;
//    if (context == null) {
//      file = await ImagePicker.pickImage(source: ImageSource.gallery);
//      return Future.value([file?.path]);
//    }
//    ClickType type = await ChooseMatisseTypeUtil.showChooseTypeDialog(
//      context,
//      maxImageSize: maxSize,
//    );
//    if (type == ClickType.camera) {
//      file = await ImagePicker.pickImage(source: ImageSource.camera);
//    } else if (type == ClickType.gallery) {
//      file = await ImagePicker.pickImage(source: ImageSource.gallery);
//    }
//    return Future.value([file?.path]);
    return await manageIosMultiImage(
        maxSize: maxSize, orientation: orientation, spanCount: spanCount);
  }

  ///ios 获取视频
  static Future<List<String>> selectIosVideo(
      {final int maxSize,
        final ScreenOrientation orientation,
        final int spanCount}) async {
    File file = await ImagePicker.pickVideo(source: ImageSource.gallery);
    return Future.value([file?.path]);
  }

  ///ios 多图上传处理
  static Future<List<String>> manageIosMultiImage(
      {final int maxSize,
        final ScreenOrientation orientation,
        final int spanCount}) async {
    List<Asset> resultAssetList = await MultiImagePicker.pickImages(
      maxImages: maxSize ?? 1,
      enableCamera: true,
      cupertinoOptions: CupertinoOptions(
        takePhotoIcon: "chat",
      ),
      materialOptions: MaterialOptions(
        actionBarColor: "#abcdef",
        actionBarTitle: "图片选择",
        allViewTitle: "所有图片",
        useDetailsView: true,
        selectionLimitReachedText: "最多选择${maxSize ?? 1}张图片",
        selectCircleStrokeColor: "#000000",
      ),
    );
    if (resultAssetList == null || resultAssetList.isEmpty) {
      return Future.value(null);
    }
    Directory rootPath = await getTemporaryDirectory();
    if (rootPath == null || rootPath.path == null || rootPath.path == "") {
      return Future.value(null);
    }
    File tmpFile;
    ByteData tmpByteData;
    List<String> finalPathList = List<String>();

    Directory tempDirectory = new Directory('${rootPath.path}/imageTemp');

    if (!tempDirectory.existsSync()) {
      tempDirectory.createSync();
    }
    for (Asset item in resultAssetList) {
      if (item == null) {
        continue;
      }
      tmpFile = new File(tempDirectory.path +
          "/choose_temp_" +
          DateTime.now().millisecondsSinceEpoch.toString() +
          ".jpg");
      tmpByteData = await item.getByteData();
      await tmpFile.writeAsBytes(tmpByteData.buffer.asUint8List());
      finalPathList.add(tmpFile.path);
    }
    return Future.value(finalPathList);
  }
}
